<?php

namespace app\zhongcan\controller;

use app\zhongcan\model\NewsModel;
use app\zhongcan\validate\NewsValidate;
use cmf\controller\AdminBaseController;

class NewsController extends AdminBaseController
{
    // 显示页面
    public function index(){
        // 获取搜索的字段
        $key = input('key');
        $where = [];
        // 判断是否有值
        if($key){
            $where[] = ['author|title', 'like', '%' . $key . '%'];
        }
        $list = NewsModel::where($where)->paginate(10);
        return $this->fetch('index',['list'=>$list]);
    }

    // 添加
    public function add(){
        $list = NewsModel::select();
        return $this->fetch('add',['list' => $list]);
    }
    // 推送添加
    public function addPost(){
        // 获取数据
        $data = input('post.');
        $content1 = htmlspecialchars_decode($data['content']);
        $content2 = cmf_replace_content_file_url($content1,true);
        $content3 = htmlspecialchars($content2);
        $data['content'] = $content3;

        // 使用验证器并判断
        $validate = new NewsValidate();
        $result = $validate->scene('add')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        // 更新时间
        $data['create_time'] = date('Y-m-d H:i:s');
        $data['update_time'] = date('Y-m-d H:i:s');

        // 添加并判断是否成功
        if (NewsModel::create($data)){
            $this->success('添加成功','News/index');
        } else {
            $this->error('添加失败');
        }
    }

    // 编辑
    public function edit(){
        $id = input('id');
        $list = NewsModel::find($id);
        $content1 = htmlspecialchars_decode($list['content']);
        $content2 = cmf_replace_content_file_url($content1,true);
        $list['content'] = $content2;
        return $this->fetch('edit',['list' => $list]);
    }

    public function editPost(){
        // 查询数据
        $data = input('post.');
        $content1 = htmlspecialchars_decode($data['content']);
        $content2 = cmf_replace_content_file_url($content1,true);
        $content3 = htmlspecialchars($content2);
        $data['content'] = $content3;

        // 验证器验证
        $validate = new NewsValidate();
        $result = $validate->scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }
        // 查询
        $list = NewsModel::find($data['id']);
        if(!$list){
            $this->error('未查询的记录');
        }
        $list->update_time = date('Y-m-d H:i:s');

        // 判断是否成功
        if($list->update($data)){
            $this->success('修改成功','News/index');
        }else{
            $this->error('修改失败');
        }
    }

    // 删除
    public function delete(){
        // 查询数据并判断数据是否存在
        $id = input('id/d','');
        $isExist = NewsModel::find($id);
        if(!$isExist){
            $this->error('删除的数据不存在');
        }

        // 删除并判断是否删除成功
        $del = NewsModel::destroy($id);
        if($del){
            $this->success('删除成功','News/index');
        }else{
            $this->error('删除失败');
        }
    }

    // 查看文章内容
    public function detail(){
        $id =input('id/d');
        if(empty($id)){
            return '参数错误';
        }
        $news = NewsModel::find($id);
        $news['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($news['content']),true);
        $this->assign('list',$news);
        return $this->fetch('detail');
    }
}